// mycomponent/yz_pay_buttons/yz_pay_buttons.js
const skinBehavior = require('../../utils/themeSkin/skinBehavior');
Component({
  behaviors: [skinBehavior],
  /**
   * 组件的属性列表
   */
  properties: {
    isMianYa: {
      type: Boolean
    },
    money: {
      type: null
    },
    buttons: {
      type: null
    },
    balance: {
      type: null
    },
    balanceNow_store: {
      type:null,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    confirmPayText: '确定',
    radio: '',
    submitArr: '',

    SubMerchantData: [],
    selectSubMerchant: null,
    selectSubMerchantIndex: 0
  },
  observers: {
    'buttons': function (_data) {
      let _selectPayType = _data[0] || {};
      if(_data[0] &&_data[0].other && _data[0].other.recharge_order_pay && _data.length > 1) {
        _selectPayType = _data[1];
      }
      this.setData({
        radio: _selectPayType.value || '',
        submitArr: _selectPayType || ''
      });
      if (_data && _data.constructor === Array && _data.length > 0) {
        _data.map(item => {
          if (item.value == 54) {
            this.setData({
              confirmPayText: item.name || '确定'
            });
          }
        });
      }
      let language = wx.getStorageSync("langIndex");
      this.setData({
        language: language.en,
      });
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onClickVal(e){
      let {name,val} = e.currentTarget.dataset;
      this.setData({
        [name]: val
      });
    },
    onClick(event){
      const { item } = event.currentTarget.dataset;
      this.setData({
        radio: item.value,
        submitArr: item
      });
      this.setSubMerchantInfo(this.data.submitArr);
    },
    changeSubMerchantID(e){
      this.setData({
        selectSubMerchantIndex: e.detail,
        selectSubMerchant: this.data.SubMerchantData[e.detail]
      })
    },
    onChange(e) {
      let _data = this.data.buttons;
      for (let key in _data) {
        if (e.detail == _data[key].value) {
          this.setData({
            radio: _data[key].value,
            submitArr: _data[key]
          });
        }
      }
      // console.log(this.data.radio,this.data.submitArr)
      this.setSubMerchantInfo(this.data.submitArr);
    },
    setSubMerchantInfo(data){
      if(data.other && data.other.child_pay && data.other.child_pay.child_pay_status ){
        let _setdata = {};
        _setdata.subMerchantPop = true;
        if(!this.data.SubMerchantData.length) _setdata.SubMerchantData = data.other.child_pay.child_pay_list;
        if(!this.data.selectSubMerchant) _setdata.selectSubMerchant = data.other.child_pay.child_pay_list[0];
        this.setData(_setdata);
      }
    },
    confirm(e) {
      this.triggerEvent('isPushHome', {
        'radio': this.data.radio,
        'submitArr': this.data.submitArr,
        'money':this.data.money,
        'child_pay_id': this.data.selectSubMerchant && this.data.selectSubMerchant.id || 0
      });
    },
    toRecharge(){
      try {
        this.ios_virtual_pay = wx.getStorageSync('ios_virtual_pay');
      } catch (e) {
        console.log(e);
      }
      if (this.ios_virtual_pay) {
        wx.showModal({
          title: '提示',
          content: '十分抱歉，由于相关规定，你暂时无法在这里充值！',
          confirmText: '知道了',
          showCancel: false
        });
        return false;
      }
      this.triggerEvent("updateData",{
        isShowRecharge: true
      });
    },
  }
});